package com.test22_日期时间;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * JDK1.8引入
 * LocalDate
 * LocalTime
 * LocalDateTime
 */
public class Demo04_LocalDate {

    @Test
    public void test1() {
        // 获取当前的日期时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("【LocalDate】" + localDate);
        System.out.println("【LocalTime】" + localTime);
        System.out.println("【LocalDateTime】" + localDateTime);
    }

    /**
     * 获取当前日期详情
     */
    @Test
    public void test2() {
        LocalDate today = LocalDate.now();
        System.out.println(String.format("【当前日期】%s-%s-%s",
                today.getYear(),
                today.getMonthValue(),
                today.getDayOfMonth()
        ));
        System.out.println("【获取星期几】" + today.getDayOfWeek().getValue());
        System.out.println("【今天所处本月的周数】" + today.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println("【今天所处本年的周数】" + today.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
        System.out.println("【今天所处本年第几天】" + today.getDayOfYear());
    }

    /**
     * 闰年判断
     */
    @Test
    public void test3() {
        // 操作特定日期
        LocalDate localDate = LocalDate.parse("1988-09-15");
        System.out.println("【闰年判断】" + localDate.isLeapYear());
        System.out.println("【获取星期几】" + localDate.getDayOfWeek());
    }

    /**
     * LocalDate日期推算
     */
    @Test
    public void test4() {
        LocalDate localDate = LocalDate.parse("1988-09-15");
        System.out.println("【所在月的第一天】" + localDate.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("【所在月的第二天】" + localDate.withDayOfMonth(2));
        System.out.println("【所在月的最后一天】" + localDate.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("【300年后的日期】" + localDate.plusYears(300));
        System.out.println("【300月后的日期】" + localDate.plusMonths(300));
        System.out.println("【所处月的第一个周一】" + localDate.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
        System.out.println("【所处年的第一个周一】" + LocalDate.parse("1988-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
    }

    /**
     * 获取指定星期的LocalDate
     */
    @Test
    public void test5() {
        LocalDate localDate = LocalDate.now();
        System.out.println("【当前日期】" + localDate);
        System.out.println("【本周一】" + localDate.with(DayOfWeek.MONDAY));
        System.out.println("【本周日】" + localDate.with(DayOfWeek.SUNDAY));
        System.out.println("=================================================");
        LocalDate lastWeekDate = localDate.plusDays(-7);
        System.out.println("【上周日期】" + lastWeekDate);
        System.out.println("【上周一】" + lastWeekDate.with(DayOfWeek.MONDAY));
        System.out.println("【上周日】" + lastWeekDate.with(DayOfWeek.SUNDAY));
        System.out.println("=================================================");
        LocalDate nextWeekDate = localDate.plusDays(7);
        System.out.println("【下周日期】" + nextWeekDate);
        System.out.println("【下周一】" + nextWeekDate.with(DayOfWeek.MONDAY));
        System.out.println("【下周日】" + nextWeekDate.with(DayOfWeek.SUNDAY));
    }

    /**
     * LocalDate、String、Date的转换
     */
    @Test
    public void test6() {
        {
            // LocalDate转String
            LocalDate localDate = LocalDate.now();
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            System.out.println(localDate.format(fmt));
            // String转LocalDate
            String str = "2022/05/20";
            System.out.println(LocalDate.parse(str, fmt));
        }
        {
            // LocalDate转Date
            LocalDate localDate = LocalDate.now();
            System.out.println(Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            // Date转LocalDate
            Date date = new Date();
            System.out.println(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        }
    }

    /**
     * LocalDateTime、String、Date的转换
     */
    @Test
    public void test7() {
        {
            // LocalDateTime转String
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
            System.out.println(localDateTime.format(fmt));
            // String转LocalDateTime
            String str = "2022/05/20 12:13:14";
            System.out.println(LocalDateTime.parse(str, fmt));
        }
        {
            // LocalDateTime转Date
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
            // Date转LocalDateTime
            Date date = new Date();
            System.out.println(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        }
    }
}
